#
# Executes commands at the start of an interactive session.
#
# Authors:
#   Sorin Ionescu <sorin.ionescu@gmail.com>
#

source_if_exists() {
  [[ -f "$1" ]] && source "$1"
}

source_if_exists /etc/zsh/zshrc

zsh_data_home="${XDG_DATA_HOME:-$HOME/.local/share}/zsh"

if [[ -d "${zsh_data_home}/site-functions" ]]; then
  fpath=("${zsh_data_home}/site-functions" $fpath)
  typeset -gU fpath
fi

source_if_exists "${zsh_data_home}/zshrc.pre"
source_if_exists "${zsh_data_home}/zsh.aliases"

source_if_exists "${HOME}/.common_aliases"

if [[ -z "$INSIDE_EMACS" ]]; then
    # Source Prezto.
    if [[ -s "${ZDOTDIR:-$HOME}/.zprezto/init.zsh" ]]; then
        source "${ZDOTDIR:-$HOME}/.zprezto/init.zsh"
    fi
else
    export PS1="%(?..[%?] )%n@%m:%1~> "
fi

source_if_exists "${zsh_data_home}/zshrc.post"

# Customize to your needs...

if (( $+commands[hstr] )); then
  # https://github.com/dvorka/hstr
  setopt histignorespace                # skip cmds w/ leading space from history
  export HSTR_CONFIG=hicolor            # get more colors
  bindkey -s "\C-r" "\C-a hstr -- \C-j" # bind hstr to Ctrl-r (for Vi mode check doc)
fi

if (( $+commands[aws_completer] )); then
  # https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-completion.html
  autoload bashcompinit && bashcompinit
  complete -C aws_completer aws # note: may slow down loading the shell
fi

if (( $+commands[direnv] )); then
  eval "$(direnv hook zsh)"
fi

autoload -Uz compinit && compinit

# Fix keymap
# commands in zle
# or http://zsh.sourceforge.net/Doc/Release/Zsh-Line-Editor.html#Standard-Widgets
autoload zkbd
source ${ZDOTDIR:-$HOME}/.zprezto/keymaps/xterm-256color-zkbd
[[ -n ${key[Home]} ]] && bindkey "${key[Home]}" beginning-of-line
[[ -n ${key[PageUp]} ]] && bindkey "${key[PageUp]}" up-line-or-history
[[ -n ${key[End]} ]] && bindkey "${key[End]}" end-of-line
[[ -n ${key[PageDown]} ]] && bindkey "${key[PageDown]}" down-line-or-history
[[ -n ${key[CtrlRight]} ]]  && bindkey "${key[CtrlRight]}" forward-word
[[ -n ${key[CtrlLeft]} ]] && bindkey "${key[CtrlLeft]}" backward-word
[[ -n ${key[Enter]} ]] && bindkey  "${key[Enter]}" accept-line

# Local Variables:
# mode: sh
# End:
